package com.sec.android.app.myfiles.module.trash;

import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.samsung.android.sdk.slinkcloud.CloudGatewayMediaStore;
import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ReverseCheckBox;

/**
 * Created by gw8951.kim on 2016-11-17.
 */

public class TrashFileListAdapterImp extends AbsFileListAdapterImp {

    public TrashFileListAdapterImp(Context context, NavigationInfo navigationInfo, AbsListViewImp listView, Cursor c, int flags) {
        super(context, navigationInfo, listView, c, flags);
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        super._bindView(context, vh, v, record, position);

        if (record instanceof TrashFileRecord) {
            TrashFileRecord trashRecord = (TrashFileRecord) record;

            boolean isShowSync = false;
            if (trashRecord.isDirectory() && TextUtils.equals(trashRecord.getTrashProcessing(), "Trash")) {
                isShowSync = true;
            }

            _bindProgressSync(context, vh, v, trashRecord, isShowSync);
        }
    }

    @Override
    protected void _bindDefaultInfoView(Context context, ListViewHolder vh, FileRecord record) {
        TextView textName = vh.getView(FileListViewHolder.NAME, TextView.class);
        if (textName != null) {
            textName.setText(record.getName());
            textName.setEllipsize(TextUtils.TruncateAt.MIDDLE);
        }

        TextView textDate = vh.getView(FileListViewHolder.DATE, TextView.class);
        if (textDate != null) {
            textDate.setVisibility(View.VISIBLE);
            textDate.setText(UiUtils.makeTimeString(mContext, record.getDate()));
        }

        TextView textSize = vh.getView(FileListViewHolder.SIZE, TextView.class);
        if (textSize != null) {
            if (record.isDirectory()) {
                textSize.setVisibility(View.GONE);
            } else {
                textSize.setVisibility(View.VISIBLE);
                textSize.setText(UiUtils.makeFileSizeString(mContext, record.getSize()));
            }
        }
    }

    @Override
    protected void _bindContentDescription(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        if (UiUtils.isSyncing(record)) {
            v.setContentDescription(context.getResources().getString(R.string.folder) + ", " + record.getName() + ", "
                    + context.getResources().getString(R.string.tts_dimmed));
        } else {
            if (record.isDirectory()) {
                v.setContentDescription(context.getResources().getString(R.string.folder) + ", " + record.getName());
            } else {
                super._bindContentDescription(context, vh, v, record, position);
            }
        }
    }

    @Override
    protected void showCheckBox(ListViewHolder vh, View v, int position) {
        super.showCheckBox(vh, v, position);
        View checkbox = vh.getView(FileListViewHolder.CHECK_BOX);

        if (checkbox != null) {
            checkbox.setAlpha(1.0f);
        }
    }

    @Override
    protected void hideCheckBox(ListViewHolder vh, View v, FileRecord record, int position) {
        View checkbox = vh.getView(FileListViewHolder.CHECK_BOX);

        v.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
        if (checkbox != null) {
            checkbox.setVisibility(View.GONE);
            if (UiUtils.needCheckableCheck(mNavigationInfo, record, mShareMode) &&  UiUtils.isSyncing(record)) {
                checkbox.setVisibility(View.VISIBLE);
                if (!isCheckable(position)) {
                    checkbox.setAlpha(0.4f);
                    if (checkbox instanceof ReverseCheckBox) {
                        if (mIsCheckable != null && mIsCheckable.size() > 0 && mIsCheckable.get(position) != null)
                            ((ReverseCheckBox) checkbox).setReverse(mIsCheckable.get(position));
                    }
                }
            }
        }
    }

    @Override
    public boolean isCheckable(int nPos) {
        boolean bRet = !UiUtils.isSyncing(getFileRecord(nPos));

        mIsCheckable.put(nPos, bRet);
        return bRet;
    }

    @Override
    public String getSelection() {
        return null;
    }

    @Override
    public String[] getSelectionArgs() {
        return null;
    }

    @Override
    protected FileRecord _getFileRecord(Cursor cursor) {
        TrashFileRecord ret = null;

        if ((cursor != null) && (cursor.getCount() > 0)) {
            int sizeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.SIZE);
            int dateIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.DATE);
            int typeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.EXT);
            int nameIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.NAME);
            int mediaIdIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.MEDIA_ID);
            int fileIdIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.FILE_ID);
            int deviceIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.DEVICE_ID);
            int linkIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.ALTERNATIVE_LINK);
            int trashProcessingIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.TRASH_PROCESSING);

            try {
                int id = cursor.getInt(mediaIdIndex);
                String name = cursor.getString(nameIndex);
                long size = cursor.getLong(sizeIndex);
                long date = cursor.getLong(dateIndex) * 1000;
                String mimeType = cursor.getString(typeIndex);
                int fileType = MediaFile.getFileTypeForMimeType(mimeType);
                if (fileType == 0) {
                    fileType = MediaFile.getFileType(name, mContext);
                }
                String fileId = cursor.getString(fileIdIndex);
                int deviceId = cursor.getInt(deviceIndex);
                int itemCount = 0;

                if ((mimeType != null) && !mimeType.isEmpty()) {
                    if (CloudGatewayMediaStore.FileBrowser.MIME_TYPE_DIR.equalsIgnoreCase(mimeType)) {
                        fileType = FileType.FOLDER;
                        int cntIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.ITEM_COUNT);
                        itemCount = cursor.getInt(cntIndex);
                    }
                }
                String trashProcessing = cursor.getString(trashProcessingIndex);

                ret = new TrashFileRecord(id, deviceId, fileId, mCurRecord.getFullPath(), name, size, date, fileType, 0, itemCount,
                        itemCount, trashProcessing);
            } catch (IllegalStateException e) {
                Log.e(this, "IllegalStateException:" + e.toString());
            } catch (CursorIndexOutOfBoundsException e) {
                Log.e(this, "CursorIndexOutOfBoundsException:" + e.toString());
            }
        }

        return ret;
    }

    @Override
    public String getFilePath(Cursor cursor) {
        return null;
    }

    @Override
    public FileRecord.StorageType getStorageType() {
        return FileRecord.StorageType.Trash;
    }

    @Override
    public boolean _isEnabled(int position) {
        return isCheckable(position);
    }
}
